# -*- coding: utf-8 -*-
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
from email.utils import parseaddr, formataddr
import smtplib

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))

# from_addr   = input('From: ')
# password    = input('password: ')
# to_addr     = input('To: ')
# smtp_server = input('SMTP server: ')
# password    = input('password: ')
password    = 'mat411516lab'
from_addr   = 'staramy_2005@126.com'
to_addr     = ['583407136@qq.com','0572395@fudan.edu.cn']
smtp_server = 'smtp.126.com'

# msg = MIMEText('hello, send by python...', 'plain', 'utf-8')
msg = MIMEMultipart('alternative')
html = """
<html>
    <body>
        <h1>Hello</h1>
        <p>send by <a href="http://www.python.org">Python</a>...</p>
        <p>send with a pic</p><br>
        <img src="cid:image1">
    </body>
</html>
"""
msg.attach(MIMEText(html, 'html', 'utf-8'))
text = "Hello\nsend by python"
msg.attach(MIMEText(text, 'plain', 'utf-8'))
msg['From']    = _format_addr('Python 爱好者<{}>'.format(from_addr))
msg['To']      = _format_addr('管理员 <{}>'.format(to_addr[0])) + ', ' + _format_addr('管理员2 <{}>'.format(to_addr[1]))
msg['Subject'] = Header('来自SMTP的问候......', 'utf-8').encode()

with open('D:/git_repo/Tulips.jpg', 'rb') as f:
    attach1 = MIMEApplication(f.read())
    attach1.add_header ('Content-Disposition', 'attachment', filename="Tulips.jpg")
    attach1.add_header ('Content-ID', '<image1>')
    msg.attach(attach1)

server = smtplib.SMTP(smtp_server, 25)
server.starttls()
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
